<template>
	<view></view>
</template>
<script>
	export default{
		data(){
			return{
				events:[],
				connected:false
			}
		},
		onLoad() {
			this.connectWebSocket()
		},
		methods:{
			connectWebSocket() {
			      uni.connectSocket({
			        url: this.baseUrl + '/sse/connect/'+ uni.getStorageSync('userId'), // SSE服务器的URL
			        success: res => {
			          console.log('WebSocket连接成功');
			          this.connected = true;
			          res.on('event', event => {
			            console.log('收到服务器事件：', event);
									this.$emit('onMessage',event)
			          });
			        },
			        fail: () => {
			          console.log('WebSocket连接失败');
			          this.connected = false;
			        }
			      });
			    }
		}
	}
</script>